package com.zj.study.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Font;
//import com.alibaba.excel.metadata.Sheet;
//import com.alibaba.excel.metadata.TableStyle;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.zj.study.entity.User;
//import com.zj.study.entity.Users;
import com.zj.study.service.UserService;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 赵赳
 * @since 2021/11/5 11:14
 * @desc 测试excel导出
 */
@RestController
public class ExcelController {

  private final UserService userService;

  public ExcelController(UserService userService) {
    this.userService = userService;
  }

  ///**
  // * excel导出
  // *
  // * @param response 响应
  // */
  //@GetMapping("/exportExcel")
  //public void export(HttpServletResponse response) throws IOException {
  //  List<User> list = userService.list();
  //  var users = list.stream().map(t -> new Users().setId(t.getId().toString())
  //      .setName(t.getName())
  //      .setAge(t.getAge().toString())).collect(Collectors.toList());
  //  ServletOutputStream out = response.getOutputStream();
  //  ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
  //  String fileName = "测试exportExcel";
  //  Sheet sheet = new Sheet(1, 0,Users.class);
  //  //设置自适应宽度
  //  sheet.setAutoWidth(Boolean.TRUE);
  //  // 第一个 sheet 名称
  //  sheet.setSheetName("第一个sheet");
  //  sheet.setTableStyle(greatTableStyle());
  //  writer.write(users, sheet);
  //  //通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
  //  response.setHeader("Content-disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
  //  writer.finish();
  //  response.setContentType("multipart/form-data");
  //  response.setCharacterEncoding("utf-8");
  //  out.flush();
  //}
  //
  //private TableStyle greatTableStyle() {
  //
  //  TableStyle tableStyle = new TableStyle();
  //  // 设置表头样式
  //  Font headFont = new Font();
  //  // 字体是否加粗
  //  headFont.setBold(true);
  //  // 字体大小
  //  headFont.setFontHeightInPoints((short)12);
  //  // 字体
  //  headFont.setFontName("楷体");
  //  tableStyle.setTableHeadFont(headFont);
  //  // 背景色
  //  tableStyle.setTableHeadBackGroundColor(IndexedColors.BLUE);
  //
  //  // 设置表格主体样式
  //  Font contentFont = new Font();
  //  contentFont.setBold(true);
  //  contentFont.setFontHeightInPoints((short)12);
  //  contentFont.setFontName("黑体");
  //  tableStyle.setTableContentFont(contentFont);
  //  tableStyle.setTableContentBackGroundColor(IndexedColors.GREEN);
  //  return tableStyle;
  //
  //}

}
